export const getFileUrl = name => {
  const url = new URL(`../assets/${name}`, import.meta.url).href

  return url
}

// 提取文本里面的http和https连接
export function extractUrls(text) {
  const urlRegex = /https?:\/\/[^\s]+/g
  const potentialUrls = text.match(urlRegex) || []

  return potentialUrls.filter(url => {
    try {
      new URL(url)
      return true
    } catch (e) {
      return false
    }
  })
}

// 提取markdown
export function extractMarkdown(markdownContent) {
  const extractedContent = markdownContent.match(/```markdown([\s\S]*?)```/)

  if (extractedContent && extractedContent[1]) {
    return extractedContent[1].trim()
  } else {
    console.log('未找到Markdown内容')
  }
}

export function saveHtmlAsFile(htmlContent, filename) {
  // 创建Blob对象，指定类型为text/html
  const blob = new Blob([htmlContent], { type: 'text/html' })

  // 创建下载链接
  const downloadLink = document.createElement('a')
  downloadLink.href = URL.createObjectURL(blob)
  downloadLink.download = filename || 'document.html'

  // 触发下载
  document.body.appendChild(downloadLink)
  downloadLink.click()

  // 清理
  setTimeout(() => {
    document.body.removeChild(downloadLink)
    URL.revokeObjectURL(downloadLink.href)
  }, 100)
}
